

AspireBid.Window = {};


AspireBid.Window.Initialize = function(self) 
	AspireBid.Window.Frame = self;
	AspireBid.Window.ItemButton = _G["ABFrames1_ItemButton" ];
	AspireBid.Window.ItemButtonTexture = _G["ABFrames1_ItemButtonTexture" ];
	AspireBid.Window.ItemLabel = _G["ABFrames1_ItemLink"];		
	AspireBid.Window.CommentField = _G["ABFrames1_InputBox"];
		
	AspireBid.Window.ItemButton:SetScript("OnEnter", function(self)
		if ( AspireBid.CurrentBid~=nil ) then			
			GameTooltip:SetOwner( AspireBid.Window.Frame, "ANCHOR_BOTTOMRIGHT", -360, 240);
			GameTooltip:SetHyperlink( AspireBid.CurrentBid );
			GameTooltip:Show();
		end
	end);
	
	AspireBid.Window.Frame:SetScript( "OnMouseDown", function( self, event ) self:StartMoving(); end);
	AspireBid.Window.Frame:SetScript( "OnMouseUp", function( self, event ) self:StopMovingOrSizing(); AspireBid.Window.SavePosition(self); end);
	AspireBid.Window.Frame:SetScript( "OnDragStop", function( self, event ) self:StopMovingOrSizing(); end);
	
	AspireBid.Window.Frame:SetFrameLevel(20);
	
	-- Able to press escape to close it
	tinsert(UISpecialFrames, "ABFrames1" );
end

AspireBid.Window.InitializeSub = function(self)
	AspireBid.Window.FrameStats = self;
	AspireBid.Window.ItemStats = _G["ABSubFrames1_ItemStats"];
	AspireBid.Window.ItemStatLabels = _G[ "ABSubFrames1_ItemStatLabels" ];
	AspireBid.Window.FrameStats:SetFrameLevel(10);	
end

AspireBid.Window.InitializeItemInfoFrame = function(self)
	AspireBid.Window.ItemInfoFrame = self;
	AspireBid.Window.ItemInfoFrameString = _G["ABItemInfoFrame_ItemString"];
	
	tinsert(UISpecialFrames, "ABItemInfoFrame" );	
end

AspireBid.Window.Toggle = function()
	if ( AspireBid.Window.Frame:IsVisible() ) then
		AspireBid.Window.Frame:Hide();
	else
		AspireBid.Window.Show(true);
	end
end

AspireBid.Window.SavePosition = function(self)
	local _, _, _, x, y = AspireBid.Window.Frame:GetPoint();
	AspireBid.BaseSetSetting("WindowPosX", x );
	AspireBid.BaseSetSetting("WindowPosY", y );	
end

AspireBid.Window.CreateSlotButtons = function( items ) 
	local frame = AspireBid.Window.Frame;
	local index = 0;
	local column = 0;
	local row = 0;
	
	-- delete current buttons
	for i,button in ipairs(AspireBid.ItemButtons) do
		button:SetParent(nil);
		button:Hide();
		AspireBid.ItemButtons[i] = nil;
	end
	AspireBid.ItemButtons = {};
	AspireBid.ItemSlots = {};
	AspireBid.ItemBid = nil;
	
	for i,item in ipairs(items) do
	
		local button = CreateFrame("CheckButton", "GearItem"..i, frame, "AB_ButtonTemplate" );
		button:SetHeight(32);
		button:SetWidth(32);
		button:SetPoint("TOPLEFT", index*35+30, -105);
		button:SetMovable(true);
		button:SetNormalTexture( item["texture"] );
		button:SetHighlightTexture('Interface\\Buttons\\ButtonHilight-Square');		
		local tex = button:GetNormalTexture();
		tex:SetHeight(32);
		tex:SetWidth(32);
		button:SetCheckedTexture('Interface\\Buttons\\ButtonHilight-Square');
		button:GetCheckedTexture():SetVertexColor( 1, 1, 0 );
		button:SetScript("OnClick", function(self, i)
			for j,k in ipairs(AspireBid.ItemButtons) do 
				if ( k == self ) then
					local item = AspireBid.ItemSlots[j];
					AspireBid.ItemBid = item;
				end
				k:SetChecked(false);				
			end		
			self:SetChecked(true);
			AspireBid.Window.SetItemUpgrade();			
		end);
		button:SetScript("OnEnter", function(self)
			for j,k in ipairs(AspireBid.ItemButtons) do
				if ( k==self ) then
					GameTooltip:SetOwner( AspireBid.Window.Frame, "ANCHOR_BOTTOMRIGHT", -375 + ((j-1)*35), 110);
					GameTooltip:SetHyperlink( AspireBid.ItemSlots[j]["link"] );
					GameTooltip:Show();
					break;
				end
			end			
		end);
		table.insert(AspireBid.ItemButtons,button);
		table.insert(AspireBid.ItemSlots,item);		
		
		if ( index == 0 and item["name"] ~= "Trophy of the Crusade" ) then
			button:SetChecked( true );
			AspireBid.ItemBid = item;
		end
		index = index + 1;
	end
		
end

AspireBid.Window.SetItemUpgrade = function( )

	if ( AspireBid.CurrentItem == nil or AspireBid.ItemBid == nil ) then
		return;
	end
	
	local stats = AspireBid.CompareItem( AspireBid.CurrentItem, AspireBid.ItemBid );
	local text = "";
	local label = "";
	
	-- _G[stat]
	--for stat, value in pairs(statDelta) do print(value, _G[stat]) end
	for stat, value in pairs(stats) do
		if ( value > 0 ) then
			text = text .. "|cff55ff55+" .. value .. "|r\n";
			label = label .. "|cff55ff55" .. _G[stat] .. "|r\n";
		end
	end
	
	for stat, value in pairs(stats) do
		if ( value < 0 ) then
			text = text .. "|cffff5555" .. value .. "|r\n";
			label = label .. "|cffff5555" .. _G[stat] .. "|r\n";
		end
	end  
	
	AspireBid.Window.ItemStats:SetText( text );
	AspireBid.Window.ItemStatLabels:SetText( label );
end

AspireBid.Window.ShowItemInfoFrame = function( )
	GameTooltip:SetHyperlink( AspireBid.CurrentBid );
	AspireBid.Window.ItemInfoFrameString:SetText( AspireBid.CurrentBid );
	AspireBid.Window.ItemInfoFrame:Show();	
end

AspireBid.Window.Show = function(forceOpen)
	
	-- load Item Details
	if ( AspireBid.CurrentBid~= nil and AspireBid.CurrentItem==nil ) then
		local item = AspireBid.ParseItem(AspireBid.CurrentBid);
		AspireBid.CurrentItem = item;
	end

	-- set Item Texture 
	if ( AspireBid.CurrentItem~=nil ) then			
		AspireBid.Window.ItemButtonTexture:SetTexture( AspireBid.CurrentItem["texture"] );
	end
	
	-- set Item Label	
	if ( AspireBid.CurrentBid~=nil ) then		
		AspireBid.Window.ItemLabel:SetText(AspireBid.CurrentBid);
	end
	
	-- add Current Items
	if ( AspireBid.CurrentItem~=nil ) then
		-- create item buttons
		local availableItems = AspireBid.GetCurrentSlots( AspireBid.CurrentItem );
		AspireBid.Window.CreateSlotButtons( availableItems );
		-- create stat difference
		AspireBid.Window.SetItemUpgrade();
	end
	
	-- erase InputBox	
	AspireBid.Window.CommentField:SetText("");
	
	if ( AspireBid.CurrentBid==nil ) then 
		if ( forceOpen ) then
			AspireBid.Print( "No Bid Currently Available." );
		else
			AspireBid.SlashCommandHandler( "default" );
		end
		
	elseif ( AspireBid.CurrentItem==nil ) then
		AspireBid.Window.ShowItemInfoFrame();
		--AspireBid.Print( " " );
		--AspireBid.Print( " >> AspireBid: Can't load item info for "..AspireBid.CurrentBid..". <<" );
		--AspireBid.Print( " >> AspireBid: Click on the item and type /abid to start the bid. <<" );
		--AspireBid.Print( " " );
		
	elseif ( forceOpen or AspireBid.CanWear(AspireBid.CurrentItem)) then
		-- Get Frame Position
		local x = AspireBid.BaseGetSetting("WindowPosX");
		local y = AspireBid.BaseGetSetting("WindowPosY");
		
		--AspireBid.Minimap.Button:ClearAllPoints();	
		-- Set Frame to the given coordinates 
		if ( x==nil or y==nil )	then AspireBid.Window.Frame:SetPoint("CENTER", 0, 0 );
		else AspireBid.Window.Frame:SetPoint("CENTER", x, y ); end
		-- Show/Hide stats frame for tokens
		if ( AspireBid.IsToken( AspireBid.CurrentItem ) ) then
			AspireBid.Window.FrameStats:Hide();
		else
			AspireBid.Window.FrameStats:Show();
		end
		-- Show the Frame
		AspireBid.Window.Frame:Show();
	end
	
end

AspireBid.Window.DoBid = function()
	-- get 
	local item = AspireBid.CurrentItem;
	
	if ( item~=nil and AspireBid.MasterLooter~=nil ) then
		local equipname = "";
		
		-- get bidded item
		if ( AspireBid.ItemBid ~= nil ) then
			equipname = AspireBid.ItemBid["link"];
		end
		
		-- get bidded item + emblems
		if ( item["name"] == "Trophy of the Crusade" ) then
			equipname = equipname .. " " .. AspireBid.GetEmblems();
		end			
		
		-- get comment		
		local comment = AspireBid.Window.CommentField:GetText();
		
		-- place bid
		SendChatMessage( equipname.." "..comment, "WHISPER", "Orcish", AspireBid.MasterLooter );
		
		-- hide frame		
		AspireBid.Window.Toggle();
	end
	
end

AspireBid.Window.DoOffspec = function()
	-- get 
	local item = AspireBid.CurrentItem;
	
	if ( item~=nil and AspireBid.MasterLooter~=nil ) then		
		
		-- get comment		
		local comment = AspireBid.Window.CommentField:GetText();
		
		-- place bid
		SendChatMessage( "offspec "..comment, "WHISPER", "Orcish", AspireBid.MasterLooter );
		
		-- hide frame		
		AspireBid.Window.Toggle();
	end
end
